ZIPURL = "http://osgworks.googlecode.com/files/osgWorks_%(VERSION)s.tar.gz"
URL = "http://osgworks.googlecode.com/svn/trunk" #svn checkout http://osgworks.googlecode.com/svn/trunk/ osgworks-read-only
VERSIONS = ["02_01_00"]
DEFAULT_VERSION = "02_01_00"
DEPENDS = ["osg"]
REQUIRES = ["svn"]
REVISION = 542

#http://osgworks.googlecode.com/svn/tags/osgWorks_03_00_00

#note after upgrading osg to a new version osgWorks should also be rebuild
#this version syncing is not managed by buildext

from source.builders import CMakeBuilder, FetchSourceSVN
import os

class FetchSourceOsgWorks(FetchSourceSVN):

  def fetch(self, url, downloadlocation, buildlocation ):
    targetdir = "osgWorks-%s" % DEFAULT_VERSION #todo allow custom version
    targetpath = buildlocation + os.sep  + targetdir
    if os.path.exists( targetpath ):
      os.system( self.executable + " update " + targetpath  )
    else:
      print self.executable + " checkout " + url + " -r " + str(REVISION) + " " + "osgworks-read-only" + " " + targetpath
      os.system( self.executable + " checkout " + url + " -r " + str(REVISION) +  " " + "osgworks-read-only" + " " + targetpath )
    return targetpath + os.sep + "trunk"

class OsgWorksBuilder( CMakeBuilder ):
  """ subclass of CMakeBuilder with custom configure step """
  
  def downloadextract(self):
    return FetchSourceOsgWorks()

  def targetpath(self):
    return self.directories.enviro + os.sep + "osgWorks_%s_osg-%s" % (self.version, self.dependencies['osg'].target.build_version)

  def configure( self ):
    """put osg directories in the environment before cmake configure"""
    
    CMAKE_CONFIGURE = r" -DCMAKE_DEBUG_POSTFIX='d'"

    #v = [int(n) for n in self.dependencies['osg'].target.build_version.split(".")]
    #if not (v[0] <= 2 and v[1] <=9  and v[2] <9):
    #  print "replacing osg::DisplaySettings::instance() (now a ref_ptr)"
    #  replacement = [( r"        masterCamera->getDisplaySettings\(\)", r"        masterCamera->getDisplaySettings() : osg::DisplaySettings::instance().get();" )]
    #  self.replaceLinesInFile(self.extractedpath + "/src/osgwTools/CameraConfigObject.cpp", replacement )
    #else:
    #  print "osg version: ", v

    #break some deprecated classes... (not used by osgbullet)
    targetfile = self.extractedpath+"\\src\\osgwTools\\CMakeLists.txt"
    replacements = [(r"    \$\{HEADER_PATH\}\/MeshOptimizers.h", ""),
                    (r"    \$\{HEADER_PATH\}\/TangentSpaceGeneratorDouble.h", ""),
                    (r"    \$\{HEADER_PATH\}\/TangentSpaceOp.h", ""),
                    (r"    MeshOptimizers.cpp", ""),
                    (r"    TangentSpaceGeneratorDouble.cpp", ""),
                    (r"    TangentSpaceOp.cpp", "")]
    self.replaceLinesInFile( targetfile, replacements )

    targetfile = self.extractedpath+"\\apps\\CMakeLists.txt"
    replacements = [(r"ADD_SUBDIRECTORY\( osgwnormalmap \)", "")]
    self.replaceLinesInFile( targetfile, replacements )

    if self.machine.isLinux():
      print "replacement in CMakeLists: debug libraries in bin folder not found"
      replacement = [( r"    \$\{EXECUTABLE_OUTPUT_PATH\}\/Debug", r"    ${EXECUTABLE_OUTPUT_PATH}" )]
      self.replaceLinesInFile(self.extractedpath + "/CMakeLists.txt", replacement )

      OSG_ROOT = ""
      OSGBuildRoot = r" -DOSGBuildRoot="+self.dependencies['osg'].targetpath()+os.sep+"lib"+ ("", "64")[self.machine.bits == 64]
      OSGSourceRoot = r" -DOSGSourceRoot="+self.dependencies['osg'].targetpath()
      OSGInstallLocation = OSGBuildRoot+OSGSourceRoot
      os.environ['OSG_DIR'] = self.dependencies['osg'].targetpath()
      #cmake generated makefiles fail to locate the libraries build in osgworks 2_1_0
      os.environ['LIBRARY_PATH'] = self.extractedpath+os.sep+'lib'

    else:
      OSG_ROOT = r" -DOSG_ROOT="+self.dependencies['osg'].targetpath()
      OSGInstallLocation = " -DOSGInstallType=\"Alternate Install Location\" -DOSGInstallLocation="+self.dependencies['osg'].targetpath()

    CMakeBuilder.configure(self, OSG_ROOT+OSGInstallLocation+CMAKE_CONFIGURE)

BUILDER = OsgWorksBuilder

